package com.yytek.nmp.controller;

import java.util.List;
import com.yytek.common.security.utils.SecurityUtils;
import com.yytek.nmp.vo.ProjectBindUserVo;
import com.yytek.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.nmp.domain.IotProjectInfo;
import com.yytek.nmp.service.IIotProjectInfoService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 项目Controller
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Api(value = "项目控制器", tags = {"项目管理"})
@RestController
@RequestMapping("/project")
public class IotProjectInfoController extends BaseController
{
    @Autowired
    private IIotProjectInfoService iotProjectInfoService;

    /**
     * 授权该项目下的人员绑定
     * @param projectBindUserVo 参数信息
     * @return
     */
    @ApiOperation("授权该项目下的人员绑定")
    @PostMapping("/authUser")
    public AjaxResult authUser(@RequestBody ProjectBindUserVo projectBindUserVo)
    {
        return toAjax(iotProjectInfoService.authProjectBindUsers(projectBindUserVo));
    }

    /**
     * 查询该项目下所有授权人员
     * @param id 项目id
     * @return
     */
    @ApiOperation("查询该项目下所有授权人员")
    @GetMapping("/getAuthUserList")
    public AjaxResult getAuthUserList(String id)
    {
        List<SysUser> list = iotProjectInfoService.getAuthUserListByProjectId(id);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询授权项目列表
     */
    @ApiOperation("分页查询授权项目列表")
    @GetMapping("/authList")
    public TableDataInfo authList()
    {
        startPage();
        String userId = SecurityUtils.getUserId().toString();
        List<IotProjectInfo> list = iotProjectInfoService.selectAuthProjectInfoListByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 查询自己所有项目列表
     */
    @ApiOperation("查询自己所有项目列表")
    @GetMapping("/myAllList")
    public AjaxResult myAllList(IotProjectInfo iotProjectInfo)
    {
        iotProjectInfo.setCreateBy(SecurityUtils.getUserId().toString());
        List<IotProjectInfo> list = iotProjectInfoService.selectIotProjectInfoList(iotProjectInfo);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询自己项目列表
     */
    @ApiOperation("分页查询自己项目列表")
    @GetMapping("/myList")
    public TableDataInfo myList(IotProjectInfo iotProjectInfo)
    {
        startPage();
        iotProjectInfo.setCreateBy(SecurityUtils.getUserId().toString());
        List<IotProjectInfo> list = iotProjectInfoService.selectIotProjectInfoList(iotProjectInfo);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @ApiOperation("导出项目列表")
    @RequiresPermissions("nmp:project:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProjectInfo iotProjectInfo)
    {
        List<IotProjectInfo> list = iotProjectInfoService.selectIotProjectInfoList(iotProjectInfo);
        ExcelUtil<IotProjectInfo> util = new ExcelUtil<IotProjectInfo>(IotProjectInfo.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @ApiOperation("获取项目详细信息")
    @RequiresPermissions("nmp:project:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotProjectInfoService.selectIotProjectInfoById(id));
    }

    /**
     * 新增项目
     */
    @ApiOperation("新增项目")
    @RequiresPermissions("nmp:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotProjectInfo iotProjectInfo)
    {
        //验证自己项目名称是否重复
        if(iotProjectInfoService.existMyProjectByName(iotProjectInfo)){
            return AjaxResult.error("项目名称重复");
        }else{
            return iotProjectInfoService.insertIotProjectInfo(iotProjectInfo);
        }
    }

    /**
     * 修改项目
     */
    @ApiOperation("修改项目")
    @RequiresPermissions("nmp:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotProjectInfo iotProjectInfo)
    {
        //验证自己项目名称是否重复
        if(iotProjectInfoService.existMyProjectByName(iotProjectInfo)){
            return AjaxResult.error("项目名称重复");
        }else{
            return toAjax(iotProjectInfoService.updateIotProjectInfo(iotProjectInfo));
        }
    }

    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @RequiresPermissions("nmp:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return iotProjectInfoService.deleteIotProjectInfoByIds(ids);
    }
}
